package com.yc.home.cloudother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bean.Result;
import com.yc.entity.Wrong;
import com.yc.home.cloudother.mapper.WrongMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-10-10
 */
@RestController
@RequestMapping("/wrong")
public class WrongController {

    @Autowired
    WrongMapper wrongMapper;

    @RequestMapping("handleSubmit")
    Result handleSubmit(int uid, int qid, String content){
        //验证今日提交次数
        LocalDateTime startOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT);
        LocalDateTime endOfDay = startOfDay.plusDays(1).minusSeconds(1);
        LambdaQueryWrapper<Wrong> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Wrong::getUid, uid);
        lqw.ge(Wrong::getTime, startOfDay);
        lqw.lt(Wrong::getTime, endOfDay);
        Long count = wrongMapper.selectCount(lqw);
        if(count > 10){
            return new Result(0, "您今日已提交10条错题，为防止恶意提交，今日次数已达上限!", null);
        }
        Wrong wrong = new Wrong();
        wrong.setUid(uid);
        wrong.setQid(qid);
        wrong.setContent(content);
        wrong.setStatus(0);
        wrongMapper.insert(wrong);
        return new Result(1, "感谢您的提交！", null);
    }

    @RequestMapping("getwrong")
    Page getwrong(@RequestParam(defaultValue = "1") int currentPage4, @RequestParam(defaultValue = "10") int pagesize, String id, String uname, String status){
        Page<Wrong> page = new Page<>(currentPage4, pagesize);
        LambdaQueryWrapper<Wrong> lqw = new LambdaQueryWrapper<>();
        if(!"".equals(id)){
            lqw.eq(Wrong::getQid, id);
        }
        if(!"".equals(uname)){
            lqw.eq(Wrong::getUid, uname);
        }
        if(!"2".equals(status)){
            lqw.eq(Wrong::getStatus, status);
        }
        return wrongMapper.selectPage(page, lqw);
    }

    @RequestMapping("look")
    Result look(String id){
        Wrong wrong = wrongMapper.selectById(id);
        wrong.setStatus(1);
        wrongMapper.updateById(wrong);
        return new Result(1, "已查看", null);
    }

}

